package com.jdls.tanhua.server;

import cn.hutool.core.collection.CollUtil;
import com.jdls.tanhua.dubbo.OptionsApi;
import com.jdls.tanhua.dubbo.QuestionnaireApi;
import com.jdls.tanhua.dubbo.TextApi;
import com.jdls.tanhua.pojo.Options;
import com.jdls.tanhua.pojo.Questionnaire;
import com.jdls.tanhua.pojo.Text;
import com.jdls.tanhua.vo.OptionsVo;
import com.jdls.tanhua.vo.QuestionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/11/8
 */
@Service
public class TestSoulServer {

    @DubboReference
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private TextApi textApi;

    @DubboReference
    private OptionsApi optionsApi;

    public List<QuestionVo> find() {

        List<Questionnaire> questionnaires = questionnaireApi.find();

        List<Integer> qid = CollUtil.getFieldValues(questionnaires, "id", Integer.class);


        Map<Integer,Text> map = textApi.find(qid);

        List<Integer> textIds= new ArrayList<>();
        for (Integer id: qid) {
            Text text = map.get(id);
            textIds.add(text.getQuestion_id());
        }

        List<Options> options = optionsApi.find(textIds);

        OptionsVo optionsVo = new OptionsVo();
        QuestionVo questionVo = new QuestionVo();

        List<QuestionVo> questionVos = new ArrayList<>();
        List<OptionsVo> optionsVos = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaires) {
            Text text = map.get(questionnaire.getId());
            for (Options option : options) {
                if(text.getId() == option.getTextId()){
                    optionsVo.setOptions(options);
                    optionsVo.setQuestion(text.getQuestion());
                    optionsVos.add(optionsVo);
                }
            }
            questionVo.setIsLock(0);
            questionVo.setQuestions(optionsVos);
            questionVo.setCover(questionnaire.getCover());
            questionVo.setLevel(questionnaire.getLevel());
            questionVo.setName(questionnaire.getName());
            questionVo.setReportId(questionnaire.getReportId());
            questionVo.setStar(Integer.valueOf(questionnaire.getStar()));
            questionVos.add(questionVo);
        }


        return questionVos;
    }
}
